//
//  ModifyUserPasswordEngine.m
//  HuLaQuan
//
//  Created by hok on 1/26/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "ModifyUserPasswordEngine.h"

#import "CurrentUser.h"

#import "NSString+MD5.h"

@implementation ModifyUserPasswordEngine
- (NSString *)description
{
   return @"修改登录密码模块";
}
/**
 *  用登陆接口来验证用户的旧密码是否正确
 *
 *  @param password 旧密码
 */
- (void)verifyOldUserPassword:(NSString *)password
{
    //登录密码加密
    NSString *pw = [RSA encryptString:password publicKey:PUBLIC_KEY];
    
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"userLogin\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><loginName>%@</loginName><loginPwd>%@</loginPwd><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,[CurrentUser sharedInstance].name,pw];
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CUSTOMER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSLog(@"item");
            result = @{@"isSuccess":@YES};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
            NSLog(@"%@",item);
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VerifyPWForModifyUserPWFinish" object:result];
    }];
}


/**
 *  修改用户登陆密码
 *
 *  @param newPassword 新密码
 */
- (void)modifyUserPassword:(NSString *)newPassword oldPassword:(NSString *)oldPW
{
    
    
    //登录密码加密
    NSString *pw = [RSA encryptString:newPassword publicKey:PUBLIC_KEY];
    
    NSString *oldpw = [RSA encryptString:oldPW publicKey:PUBLIC_KEY];
    
    NSLog(@"%@, %@, %@, %@",newPassword,oldPW,pw,oldpw);
    
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"modifyLoginPwd\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><loginPwd>%@</loginPwd><loginPwdNew>%@</loginPwdNew><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,oldpw,pw];
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CUSTOMER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            result = @{@"isSuccess":@YES};
            
        }else{
            NSLog(@"%@",item);
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ModifyUserPasswordFinish" object:result];
    }];

}
@end
